// 定义接口方法
import request from '@/utils/request'

/**
 * 登录接口
 * @param {*} param0 数据对象
 * @returns Promise对象
 */
export const loginAPI = ({ mobile, code }) => request({
  url: '/v1_0/authorizations',
  method: 'POST',
  data: {
    mobile,
    code
  }
})

/**
 * 获取所有频道
 * @returns Promise对象
 */
export const getChannelListAPI = () => request({
  url: '/v1_0/channels'
})

// 获取用户的频道
/**
 * 获取已选频道
 * @returns Promise对象
 * 注意: 不强制要求用户登录, 匿名用户有默认的频道列表
 */
export const getUserChannelListAPI = () => request({
  url: '/v1_0/user/channels'
  // method: 'GET'
  /* headers: {
    Authorization: `Bearer ${getToken()}`
  } */
})

/**
 * 获取首页-文章列表
 * @param {*} channel_id 频道id
 * @returns Promise对象
 * timestamp = new Date()形参位置设置默认值
 */
export const getArticleListAPI = ({ channel_id, timestamp = new Date() }) => request({
  url: '/v1_0/articles',
  method: 'GET',
  /*  headers: {
    Authorization: `Bearer ${getToken()}`
  }, */
  params: {
    // 传参的情况：调用的时候，这个值会变，必须要留好参数，让调用者决定
    channel_id,
    // 函数内能自己确定的，表示这个值是固定的，就不用传参
    timestamp
  }
})

// 搜索-结果列表
export const seachResultAPI = ({ keywords, page = 1, per_page = 10 }) => request({
  url: '/v1_0/search',
  params: {
    q: keywords,
    page,
    per_page
  }
})

// 重置频道(覆盖式保存)
// RestFul API规范 (地址相同, 请求方式不同, 动作和功能不同)
export const setUserChannelAPI = (channelArr) => request({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: {
    // 数组套对象发给后台 -> 转成了JSON字符串发给后台
    // 对象套对象也能转成JSON字符串格式 => 后台
    channels: channelArr
  }
})

// 删除频道
export const removeChannelAPI = (channelId) => request({
  url: `/v1_0/user/channels/${channelId}`,
  method: 'DELETE'
})

// 封装首页反馈--不喜欢文章的请求方法
export const dislikeArticleAPI = (artId) => request({
  url: '/v1_0/article/dislikes',
  method: 'POST',
  /* headers: {
    Authorization: `Bearer ${getToken()}`
  }, */
  data: {
    target: artId
  }
})

// 首页-反馈-举报文章请求方法
export const reportsArticleAPI = ({ artId, type }) => request({
  url: '/v1_0/article/reports',
  method: 'POST',
  /*  headers: {
    Authorization: `Bearer ${getToken()}`
  }, */
  data: {
    target: artId,
    type,
    remark: '就是其他文章'
    // 思路: 点击其他问题, 显示一个输入框, 点击确定的时候, 再调用接口
  }
})

// 获取文章详情
export const articleDetailAPI = ({ artId }) => request({
  url: `/v1_0/articles/${artId}`
})

// 文章-关注作者
export const followedAuthorAPI = ({ authorId }) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: authorId
  }
})

// 文章-取消关注作者
export const unFollowedAuthorAPI = (authorId) => request({
  // 路径传参
  // Path： /v1_0/user/followings/:target
  url: `/v1_0/user/followings/${authorId}`,
  method: 'DELETE'
})

// 文章-点赞
export const likeArticleAPI = (artId) => request({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: artId
  }
})

// 文章-已点赞
export const unlikeArticleAPI = (artId) => request({
  url: `/v1_0/article/likings/${artId}`,
  method: 'DELETE'
})

// 搜索联想列表
export const suggestListAPI = (kewWord) => request({
  url: '/v1_0/suggestion',
  params: {
    q: kewWord
  }
})

// 获取评论数据
export const getCommentListAPI = ({ artId, offset = null, limit = 10 }) => request({
  url: '/v1_0/comments',
  // method: 'GET'
  params: {
    type: 'a',
    source: artId,
    offset: offset,
    limit: limit
  }
})

// 对评论恢复点赞
export const likeCommentListAPI = (commentId) => request({
  url: '/v1_0/comment/likings',
  method: 'POST',
  data: {
    target: commentId
  }
})

// 取消对评论回复点赞
export const unLikeCommentListAPI = (commentId) => request({
  url: `/v1_0/comment/likings/${commentId}`,
  method: 'DELETE'
})

// 底部评论
export const sendCommentAPI = ({ target, content, artId = null }) => {
  const obj = {
    // 文章ID, 或评论ID
    target: target,
    content: content
  }
  if (artId !== null) {
    obj.art_id = artId
  }
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj
  })
}

// 获取用户自己的信息
export const getUserInfoAPI = () => request({
  url: '/v1_0/user'
})

// 获取用户个人资料
export const getUserProfileAPI = () => request({
  url: '/v1_0/user/profile'
})

// 编辑用户头像
export const uploadHeadImgAPI = (fd) => request({
  url: '/v1_0/user/photo',
  method: 'PATCH',
  data: {
    photo: fd
  }
})
